/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.boundary;

import giuliani_mapelli_marocco.business.UserManager;
import giuliani_mapelli_marocco.business.EventManager;
import giuliani_mapelli_marocco.business.TimeTools;
import giuliani_mapelli_marocco.entity.Event;
import giuliani_mapelli_marocco.entity.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

@Named
@ViewScoped
public class OtherCalendarBean implements Serializable{

    /**
     * Creates a new instance of OtherCalendarBean
     */
    public OtherCalendarBean() {
    }
    
    @EJB
    UserManager um;
    
    @EJB
    EventManager em;
    
    @PersistenceContext
    EntityManager enm;
    
    
    private ScheduleModel eventModel; 
    private ScheduleEvent scheduleEvent = new DefaultScheduleEvent();
    private Event event;
    private String username;
    private User user;
 
    /**
     * Initializes the calendar, adding all the events that the user have created
     * and in which it participates.
     */
    @PostConstruct
    public void init() {
        String eventName;
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance()
                .getExternalContext().getRequest();
        username=req.getParameter("username");
        if(username != null) {
        eventModel = new DefaultScheduleModel();
        TimeTools t = new TimeTools();
        List<Event> events=  em.getAllUserEvent(username);
        for(Event e: events) {
            if(!(e.getDeleted())) {
                Date startDate = t.mergeDateAndHour(e.getStartDate(), e.getStartTime());
                Date endDate = t.mergeDateAndHour(e.getEndDate(), e.getEndTime());
                if(e.getIsPublic()) {
                    eventName = e.getName();
                } else {
                    eventName = "busy";
                }
                DefaultScheduleEvent defEvent = new DefaultScheduleEvent(eventName, startDate, endDate);
                defEvent.setData(e.getId());
                eventModel.addEvent(defEvent);
                }
            }
        }
    }
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public ScheduleModel getEventModel() {
        return eventModel;
    }
     
    public ScheduleEvent getScheduleEvent() {
        return scheduleEvent;
    }
 
    public void setScheduleEvent(ScheduleEvent scheduleEvent) {
        this.scheduleEvent = scheduleEvent;
    }
    
    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }
    
    public User getUser() {
        return user;
    }

    /**
     * Handle the "event select" events, saving the selected event in the bean.
     * @param selectEvent event selected
     */
    public void onEventSelect(SelectEvent selectEvent) {
        scheduleEvent = (ScheduleEvent) selectEvent.getObject();
        event = em.findEventById((Long)scheduleEvent.getData());
    }
}
